<!--
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 14:41:20
 * @LastEditTime: 2023-09-01 15:16:10
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description:   :gridOps="gridOptions" other
-->

<template>
	<sc-VxeTable ref="xGrid" :gridEvts="gridEvents" :tablecode="'pro_plan_index'" :params="props.params"
		:url="api.pro.plan.list">
	</sc-VxeTable>
	<SaveDialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></SaveDialog>
</template>

<script>
export default {
	name: "pro_plan_index",
};
</script>

<script setup>
// import { useRouter } from "vue-router";
import { ElNotification } from "element-plus";
import { ref, nextTick } from "vue";
// , onMounted, reactive, ref
// const router = useRouter();
import SaveDialog from "./save";
import api from "@/api";
const dialog = ref({
	save: false,
});
const props = defineProps({
	params: {
		type: Object,
		default: () => {
			return { plan_id: 1 };
		},
	},
});
const emit = defineEmits(["success"]);
const xGrid = ref({});
const saveDialog = ref({});
const handleSuccess = () => {
	emit("success");
	xGrid.value.refresh();
};
const formatterData = function (selectData) {
	let form = {};
	let lotName = [];
	let companyName = [];
	let locationName = [];
	let showDialog = true;
	form.material_id = selectData[0].material_id;
	form.material = selectData[0].material;


	form.loom_id = selectData[0].loom_id;
	form.loom = selectData[0].loom;
	form.ratio = parseFloat(form.loom.ratio);
	form.dosage = form.loom.dosage;
	//投料比
	form.feeding_ratio = 0;

	form.yarn_quantity = 0;

	form.color = selectData[0].color;
	form.color_id = selectData[0].color_id;
	form.quantity = 0;
	form.remark = selectData[0].remark;
	form.dgremark = selectData[0].dgremark;

	selectData.map((item) => {
		if (
			form.material_id != item.material_id ||
			form.color_id != item.color_id
		) {
			ElNotification.error({
				title: "错误",
				message: "相同产品/颜色才可以并缸！",
			});
			showDialog = false;
		}
		if (form.loom_id != "" && form.loom_id != item.loom_id) {
			form.loom = {};
			form.loom_id = "";
			form.ratio = 0;
			form.bath_qty = 0;
		}

		if (lotName.indexOf(item.lot_name) == -1) {
			lotName.push(item.lot_name);
		}
		if (companyName.indexOf(item.company?.name) == -1) {
			companyName.push(item.company?.name);
		}
		if (locationName.indexOf(item.location?.name) == -1) {
			locationName.push(item.location?.name);
		}
		//订单
		form.quantity += parseFloat(item.pro_quantity);
		//白纱重
		form.yarn_quantity += (item.feeding_ratio * item.pro_quantity) / 1000;
	});
	//投料比
	form.feeding_ratio = (form.yarn_quantity / form.quantity) * 1000;
	//批号
	form.lot_name = lotName.join();
	//客户
	form.company_name = companyName.join();
	//库位
	form.location_name = locationName.join();
	if (showDialog) {
		dialog.value.save = true;
		nextTick(() => {
			saveDialog.value.setData(form, selectData);
			saveDialog.value.open("edit");
		});
	}
};
const gridEvents = {
	cellDblclick: function ({ row }) {
		formatterData([row]);
	},
	add: function () {
		dialog.value.save = true;
		nextTick(() => {
			saveDialog.value.open();
		});
	},
	edit: function (data) {
		formatterData(data.button.selectRecords);
	},
};
</script>
